<?php
class Goods{
	
	function __construct(){
		
	}

	function orderdone($data){
		global $conf;
		$db = new db($conf);
		$oid = isset($data['oid'])?intval($data['oid']):0;
		$info = userinfo();
		$oinfo = $db->field(['*'])->where("Id=$oid and uid=".$info['Id']." and td=0 and state=3")->select("sc_orders");
		if(!$oinfo){ error('未查询到订单信息');}		
		$up = $db->field(['*'])->where("Id=".$oinfo[0]['Id'])->update("sc_orders",['state'=>4,'endtime'=>date("Y-m-d H:i:s")]);			
		if($up){
			success('收货成功');
		}else{
			error('操作失败');
		}
	}

	function orderback($data){
		global $conf;
		$db = new db($conf);
		$trackname1 = isset($data['t0'])?trim($data['t0']):"";
		$trackcode1 = isset($data['t1'])?trim($data['t1']):"";
		$oid = isset($data['oid'])?trim($data['oid']):0;
		$info = userinfo();
		if($oid<=0){ error('请选择要回寄的订单');}
		if(!$trackname1 || !$trackcode1){ error('请填写回寄订单快递信息');}
		$oinfo = $db->field(['*'])->where("Id=$oid and uid=".$info['Id']." and cplx=2 and zh=0 and jhstate=0 and state=4 and td=0")->select("sc_orders");
		if(!$oinfo){ error('未查询到可寄回订单信息');}
		
		$up = $db->field(['*'])->where("Id=".$oinfo[0]['Id'])->update("sc_orders",['jhstate'=>1,'jhtime'=>date("Y-m-d H:i:s"),'trackname1'=>$trackname1,'trackcode1'=>$trackcode1]);
		if($up){
			success('操作成功');
		}else{
			error('回寄失败');
		}
	}

	function hexiao($data){
		global $conf;
		$db = new db($conf);
		$hxcode = isset($data['oid'])?trim($data['oid']):0;
		if(strlen($hxcode)!=8){ error("请输入8位核销码");}
		$info = userinfo();
		if($info['hx']==0){ error("权限不足");}
		$oinfo = $db->field(array("*"))->where("hxcode='".$hxcode."' and state=1")->select("sc_orders");
		if(!$oinfo){ error("未查询到订单信息");}
		$up = $db->where("Id=".$oinfo[0]['Id'])->update("sc_orders",array("state"=>3,"trackname"=>"平台核销","trackcode"=>1,"endtime"=>date("Y-m-d H:i:s"),"hxid"=>$info['Id']));
		if($up){
			if($oinfo[0]['hxcash']>0){
				$jjcash = $oinfo[0]['hxcash'];
				$db->where("Id=".$info['Id'])->update("sc_member",['jjcash'=>"(jjcash + $jjcash)",'jjcash1'=>"(jjcash1 + $jjcash)"]);
				$db->insert("sc_cash",array("uid"=>$info['Id'],"cash"=>$jjcash,"addtime"=>date("Y-m-d H:i:s"),"type"=>13,"adda"=>1));
			}
			success("核销成功");			
		}else{
			error("核销失败");
		}
	}

	function orderc($data){
		global $conf;
		global $results;
		$db = new db($conf);
		$gid = isset($_POST['gid'])?$_POST['gid']:"";
		$num = isset($_POST['num'])?$_POST['num']:"";
		$type = 2;
		$pay = isset($_POST['pay'])?intval($_POST['pay']):0;
		$dzid = isset($_POST['dzid'])?intval($_POST['dzid']):"";
		$color = isset($_POST['c'])?$_POST['c']:"";	
		$size = isset($_POST['s'])?$_POST['s']:"";
		$ticket = isset($_POST['ticket'])?$_POST['ticket']:"";
		if($pay<1 || $pay>4){ error("请选择支付方式");}
		if(count($gid)==count($num) && count($gid)==count($ticket) && count($gid)==count($size) && count($gid)>0){		
		}else{		
			error("提交出错");
		}
		$info = userinfo();
		if(!$info){ error("账户异常");}
		$dz = $db->field(array("*"))->where("Id=$dzid and uid=".$info['Id'])->select("sc_address");
		if(!$dz){ error("请选择收货地址");}
		$r1 = $db->field(array("*"))->where("areaid in(".$dz[0]['province'].",".$dz[0]['city'].",".$dz[0]['county'].")")->order("areaid asc")->select("sc_area");		
		if(count($r1)==2){
			$address = $r1[0]['areaname'].$r1[1]['areaname'].$dz[0]['address'];
			$pro = $r1[0]['areaid'];
			$cit = $r1[0]['areaid'];
			$cou = $r1[1]['areaid'];
			$tname = $dz[0]['tname'];
			$phone = $dz[0]['phone'];
		}elseif(count($r1)==3){
			$address = $r1[0]['areaname'].$r1[1]['areaname'].$r1[2]['areaname'].$dz[0]['address'];
			$pro = $r1[0]['areaid'];
			$cit = $r1[1]['areaid'];
			$cou = $r1[2]['areaid'];
			$tname = $dz[0]['tname'];
			$phone = $dz[0]['phone'];
		}else{
			error("地址异常");
		}
		
		$goods = [];
		$all = $alllj = $cplx = $gtype = $oid = 0;
		$content = "";
		//获取系统设置
		$set = set();
		for($i=0;$i<count($gid);$i++){
			$ginfo = $db->field(array("*"))->where("Id=".$gid[$i])->select("sc_goods");
			if(!$ginfo) {error("购物车已更新 请刷新");}
			if($ginfo[0]['gstate']==0){error("产品 ".$ginfo[0]['gstate']." 已下架");}
			if($ginfo[0]['gtotal']<$num[$i]){error("产品 ".$ginfo[0]['gstate']." 库存不足");}
			
			$goods[$i]['id'] = $ginfo[0]["Id"];
			$goods[$i]['name'] = $ginfo[0]["gname"];
			
			$goods[$i]['thumb'] = $ginfo[0]["gthumb"];
			$goods[$i]['num'] = $num[$i];
			if($cplx==0){
				$cplx = $ginfo[0]['cplx'];				
			}elseif($cplx!=$ginfo[0]['cplx']){
				error('不同专区产品请分开下单');
			}
			if($cplx==2){
				if($num[$i]>1 || $i>0){ error('置换区一次限购一件');}
				$gtype = $ginfo[0]['type'];
				if($pay==4){
					// 接收前端选择的置换订单ID
					$selected_oid = isset($_POST['exchange_oid'])?intval($_POST['exchange_oid']):0;
					if($selected_oid > 0){
						// 验证该订单是否可置换
						$oinfo = $db->field(['*'])->where("Id=$selected_oid and uid=".$info['Id']." and gtype=$gtype and zh=0 and jhstate>0 and state=4 and td=0")->select("sc_orders");
						if(!$oinfo){ error('所选订单不可置换');}
						$oid = $selected_oid;
					}else{
						// 如果前端没传，则使用旧逻辑（自动选择第一个）
						$oinfo = $db->field(['*'])->where("uid=".$info['Id']." and gtype=$gtype and zh=0 and jhstate>0 and state=4 and td=0")->order('Id asc')->select("sc_orders");
						if(!$oinfo){ error('未查询到可置换的订单');}
						$oid = $oinfo[0]['Id'];
					}
				}				
			}else{
				if($info['level']==0){ error('仅限报单会员购买');}
			}
			$goods[$i]['price'] = $ginfo[0]["cash"];
			$goods[$i]['kdf'] = $ginfo[0]["kuaidif"];			
			
			$content = $content.$goods[$i]['name']."(X".$num[$i].") 单价：".$goods[$i]['price'];
			if(count($gid)>1 && $i<count($gid)-1){
				$content = $content."<br />";
			}
			$all = $all + $goods[$i]['price']*$num[$i] + $goods[$i]['kdf']*$num[$i];		
			$alllj += $goods[$i]['price']*$num[$i];			
		}	
		$ocode = "AL".time().mt_rand(100000,999999);
		$goods = json_encode($goods);
		if($pay==4){
			$all = $all - $alllj;
			$alllj = 0;
		}else{
			$all = $alllj;
		}
		if($pay==1 || $pay==4){
			
			if($info['cash']<$all){ 
				error("余额不足，当前剩余：".$info['cash']);
			}			
			$ufield['cash'] = "(cash - $all)";				
			$up = $db->where("Id=".$info['Id']." and state=1")->update("sc_member",$ufield);							
			if($up){
				try{											
					$db->insert("sc_payorder",array("uid"=>$info['Id'],"cash"=>$all,"alllj"=>$alllj,"goods"=>$goods,"cplx"=>$cplx,"pay"=>$pay,"state"=>1,"addtime"=>date("Y-m-d H:i:s"),"paytime"=>date("Y-m-d H:i:s"),"type"=>$type,"ocode"=>$ocode,"address"=>$address,"pro"=>$pro,"cit"=>$cit,"cou"=>$cou,"tname"=>$tname,"phone"=>$phone,"content"=>$content,"gtype"=>$gtype,"oid"=>$oid));
					$oid = $db->done("LAST_INSERT_ID()");
					$oid = $oid[0]['LAST_INSERT_ID()'];
				}catch(Exception $e){					
					$db->where("Id=".$info['Id']." and state=1")->update("sc_member",array("cash"=>"(cash + $all)"));
					error("提交订单出错");	
				}
				if($cplx==2 && $pay==4){
					$up = $db->where("Id=".$oinfo[0]['Id'])->update("sc_orders",['zhtime'=>date("Y-m-d H:i:s"),'zh'=>1]);
				}else{
					$up = true;
				}
				if($up){
					notify($oid);
					success("购买成功");
				}else{
					$db->where("Id=".$info['Id']." and state=1")->update("sc_member",array("cash"=>"(cash + $all)"));
					error("下单失败");
				}														
			}else{
				error("下单失败");
			}
		}elseif($pay==2){
 			// error('支付功能开发中');	
			if($all<=0){ error("请选择余额支付");}
			$db->insert("sc_payorder",array("uid"=>$info['Id'],"cash"=>$all,"alllj"=>$alllj,"goods"=>$goods,"cplx"=>$cplx,"pay"=>$pay,"state"=>0,"addtime"=>date("Y-m-d H:i:s"),"type"=>$type,"ocode"=>$ocode,"address"=>$address,"pro"=>$pro,"cit"=>$cit,"cou"=>$cou,"tname"=>$tname,"phone"=>$phone,"content"=>$content,"gtype"=>$gtype));			
			$data = array("money"=>$all*100,"ordercode"=>$ocode,"title"=>$info['uname'],"content"=>"购买商品","type"=>2);
			include($_SERVER['DOCUMENT_ROOT']."/wechat/pay.php");
			$wechat = new wechatpay($data);
			$_SESSION['ordercode'] = $ocode;
			if($wechat->is_weixin()){
				$str = array(7,"/wechat/jsapi.php"); echo json_encode($str);
			}else{
				$pay = $wechat->wxpaymoney();$str = array(6,$pay); echo json_encode($str);
			}
		}elseif($pay==3){
 			// error('支付功能开发中');	
			if($all<=0){ error("请选择余额支付");}
			$db->insert("sc_payorder",array("uid"=>$info['Id'],"cash"=>$all,"alllj"=>$alllj,"goods"=>$goods,"cplx"=>$cplx,"pay"=>$pay,"state"=>0,"addtime"=>date("Y-m-d H:i:s"),"type"=>$type,"ocode"=>$ocode,"address"=>$address,"pro"=>$pro,"cit"=>$cit,"cou"=>$cou,"tname"=>$tname,"phone"=>$phone,"content"=>$content,"gtype"=>$gtype));		
			$_SESSION['ordercode'] = $ocode;
			$str = array(5,"/alipay/wappay/pay.php");echo json_encode($str);	
		}		
	}

	//清除购购物车产品
	function cartdel($data){
		global $conf;
		$db = new db($conf);
		$id = isset($data['id'])?intval($data['id']):0;
		if($id){
			$result = $db->where(array("Id"=>$id,"uid"=>$_SESSION['userid']))->delete("sc_buycart");
			if($result){
				$str = array(1,"移除成功");
				echo json_encode($str);
			}else{
				$str = array(1,"移除商品失败，请刷新");
				echo json_encode($str);
			}
		}
	}

	//加入购物车
	function cart($data){
		global $conf;
		$db = new db($conf);
		$id = isset($data['id'])?intval($data['id']):0;
		$num = 1;
		$color = isset($data['c'])?$data['c']:"";
		$size = isset($data['s'])?$data['s']:"";
		$ticket = isset($data['ticket'])?$data['ticket']:"";
		if(empty($id)){ error("信息异常");}
		if($num<1){ error("选择产品数量异常");}
		$ginfo = $db->field(array("*"))->where(array("Id"=>$id))->select("sc_goods");
		if($ginfo){
			$info = userinfo();
			if($ginfo[0]['gstate']==0){ error("产品已下架");}			
			if($num>$ginfo[0]['gtotal']){ error("库存不足，请联系商家");}
			$bresult = $db->field(array("*"))->where(array("uid"=>$_SESSION['userid'],"gid"=>$id,'ticket'=>'"'.$ticket.'"'))->select("sc_buycart");
			if($bresult){
				success("已加入购物车");
			}else{
				$db->where("uid=".$_SESSION['userid']." and cplx <> ".$ginfo[0]['cplx'])->delete("sc_buycart");			
				$field = array("uid"=>$_SESSION['userid'],"gid"=>$id,"num"=>$num,"ticket"=>$ticket,"cplx"=>$ginfo[0]['cplx']);
				$add = $db->insert("sc_buycart",$field);			
				$str = array(3,"已加入购物车");echo json_encode($str);exit;		
			}				
		}else{
			error("未获取到产品信息");
		}
	}

}
?>